﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pattern18观察者模式.Base
{
    /// <summary>
    /// 被观察者的抽象基类
    /// </summary>
    public abstract class Subject
    {
        private IList<Observer> _observers = new List<Observer>();//当前主题对象的观察者集合

        /// <summary>
        /// 添加观察者
        /// </summary>
        /// <param name="observer"></param>
        public void Attach(Observer observer)
        {
            _observers.Add(observer);
        }

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="observer"></param>
        public void Detach(Observer observer)
        {
            _observers.Remove(observer);
        }

        public void Nofity()
        {
            Console.WriteLine("猫突然大叫一声...喵...");

            foreach (var item in _observers)
            {
                item.Update();
            }
        }
    }
}
